package view.player
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;
    
    import memory.*;
    
    import mx.core.*;
    import mx.messaging.channels.*;

    public class SerialSwfPlayer extends UIComponent
    {
        private var swfUrls:Array;
        private var swfs:Array;
        private var loader:Loader;
        private var bgImage:Bitmap;
        private var bgImageUrl:String;

        public function SerialSwfPlayer(bgImageUrl:String,swfUrls:Array)
        {
            swfs = new Array();
            this.bgImageUrl = bgImageUrl;
            this.swfUrls =swfUrls ;
            return;
        }// end function

        private function on_swf_loaded(NetConnectionChannel:Event) : void
        {
            loader.x = swfUrls[0].x ? (swfUrls[0].x) : (0);
            loader.y = swfUrls[0].y ? (swfUrls[0].y) : (0);
            loader.scaleX = swfUrls[0].scaleX ? (swfUrls[0].scaleX) : (1);
            loader.scaleY = swfUrls[0].scaleY ? (swfUrls[0].scaleY) : (1);
            MemoryRecovery.getInstance().gcFun(loader.contentLoaderInfo,Event.COMPLETE, on_swf_loaded);
            addChild(loader);
            swfs.push(loader);
            swfUrls.shift();
            loadAllSwf();
            return;
        }// end function

        private function loadAllSwf() : void
        {
            if (swfUrls != null && swfUrls.length != 0)
            {
            	var context:LoaderContext=new LoaderContext(false,ApplicationDomain.currentDomain);
                loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_swf_loaded);
                loader.load(new URLRequest(swfUrls[0].url),context);
            }
            else
            {
                dispatchEvent(new SerialSwfPlayerEvent(SerialSwfPlayerEvent.COMPLETE));
            }
            return;
        }// end function

        private function on_image_loaded(NetConnectionChannel:Event) : void
        {
            bgImage = Bitmap(loader.content);
            addChild(bgImage);
            dispatchEvent(new SerialSwfPlayerEvent(SerialSwfPlayerEvent.READY));
            loader = null;
            loadAllSwf();
            return;
        }// end function

        public function start() : void
        {
            if (bgImageUrl != null && bgImageUrl != "")
            {
                loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_image_loaded);
                loader.load(new URLRequest(bgImageUrl),new LoaderContext(true));
            }
            return;
        }// end function

        public function resume() : void
        {
            var _loc_1:Loader;
            for each (_loc_1 in swfs)
            {
                
                if (_loc_1.content is MovieClip)
                {
                    MovieClip(_loc_1.content).play();
                }
            }
            return;
        }// end function

        public function pause() : void
        {
            var _loc_1:Loader;
            for each (_loc_1 in swfs)
            {
                
                if (_loc_1.content is MovieClip)
                {
                    MovieClip(_loc_1.content).stop();
                }
            }
            return;
        }// end function

        public function dispose() : void
        {
            var _loc_1:Loader;
            if (loader != null)
            {
                MemoryRecovery.getInstance().gcFun(loader.contentLoaderInfo, Event.COMPLETE, on_swf_loaded);
                MemoryRecovery.getInstance().gcFun(loader.contentLoaderInfo, Event.COMPLETE, on_image_loaded);
                loader.unload();
                if (loader.parent != null)
                {
                    loader.parent.removeChild(loader);
                }
                loader = null;
            }
            if (bgImage != null)
            {
                if (bgImage.bitmapData != null)
                {
                    bgImage.bitmapData.dispose();
                    bgImage.bitmapData = null;
                }
            }
            MemoryRecovery.getInstance().gcObj(bgImage);
            bgImage = null;
            for each (var loader:Loader in swfs)
            {
                if(loader.content.hasOwnProperty("dispose"))
                {
                	loader.content["dispose"]();
                }
                MemoryRecovery.getInstance().gcFun(loader.contentLoaderInfo, Event.COMPLETE, on_swf_loaded);
                if(loader.parent!=null)
                {
                	loader.parent.removeChild(loader);
                }
                loader.unload();
                loader = null;
            }
            swfs = null;
            MyGC.gc();
            return;
        }// end function

    }
}
